package com.bhs.sansonglogistics.ui.order;

import static android.content.Context.CLIPBOARD_SERVICE;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.OrderGoodsBean;
import com.bhs.sansonglogistics.dialog.MyClickListener;
import com.bhs.sansonglogistics.dialog.ViewCancelDialog;
import com.bhs.sansonglogistics.net.Config;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.OrderGoodsAdapter;
import com.bhs.sansonglogistics.ui.home.OrderGoodsListFragment;
import com.bhs.sansonglogistics.ui.other.WebViewActivity;
import com.bhs.sansonglogistics.ui.waybill.WaybillDetailsActivity;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 订单按钮的操作
 */
public class OrderOperation implements NetCallBack {

    private ClipData mClipData;   //剪切板Data对象
    private ClipboardManager mClipboardManager;   //剪切板管理工具类
    private BaseActivity activity;
    private String type;

    public OrderOperation(BaseActivity activity, String type) {
        this.activity = activity;
        // 1. 注册mClipboardManager
        mClipboardManager = (ClipboardManager) activity.getSystemService(CLIPBOARD_SERVICE);
        this.type = type;
    }

    public OrderOperation(BaseActivity activity) {
        this.activity = activity;
        // 1. 注册mClipboardManager
        mClipboardManager = (ClipboardManager) activity.getSystemService(CLIPBOARD_SERVICE);
    }

    public void operation(OrderGoodsAdapter orderGoodsAdapter) {
        orderGoodsAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                OrderGoodsBean data = (OrderGoodsBean) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.tv_order_number:
                        //创建一个新的文本clip对象
                        mClipData = ClipData.newPlainText("Simple text", data.getDeliver_sn());
                        //把clip对象放在剪贴板中
                        mClipboardManager.setPrimaryClip(mClipData);
                        ToastUtil.show("复制成功");
                        break;
                    case R.id.btn_confirm_receipt:
                    case R.id.btn_confirm_departure:
                    case R.id.btn_confirm_arrival:
                    case R.id.btn_confirm_sign_for:
                    case R.id.btn_receipt_pending:
                    case R.id.tv_sign_for:
                    case R.id.tv_receipt:
                        changeStatus(data);
                        break;
                    case R.id.btn_confirm_cancel:
                        confirmCancel(data);
                        break;
                    case R.id.btn_incremental_expenses:
                        activity.startActivity(new Intent(activity, IncrementalExpensesActivity.class)
                                .putExtra("deliver_sn", data.getDeliver_sn())
                                .putExtra("type", type));
                        break;
                    case R.id.btn_waybill:
                        activity.startActivity(new Intent(activity, WaybillDetailsActivity.class)
                                .putExtra("waybill_sn", data.getWaybill_sn())
                                .putExtra("status", type));
                        break;
                    case R.id.btn_cash_register:
                        cashRegister(data);
                        break;
                    case R.id.btn_view_cancel:
                        new XPopup.Builder(activity)
                                .asCustom(new ViewCancelDialog(activity, data.getCancel_info()))
                                .show();
                        break;
                    case R.id.btn_claim_goods:
                    case R.id.tv_claim_goods:
                        MyUtils.weChatMiniProgram(activity);
                        break;
                    case R.id.btn_electronic_order:
                        String url = Config.agreementUrl + "index?type=android&deliver_sn=" + data.getDeliver_sn();
                        //String url = "http://192.168.1.21:8080/#/?deliver_sn=SS2310310000004&type=android";
                        //String url = "file:///android_asset/new_file.html";
                        activity.startActivity(new Intent(activity, WebViewActivity.class)
                                .putExtra("url", url)
                                .putExtra("title", "电子面单"));
                        break;
                }

            }
        });

    }

    private void cashRegister(OrderGoodsBean data) {
        new XPopup.Builder(activity)
                .asConfirm("提示", "确定已经现金收款吗",
                        "取消", "确定",
                        new OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                if (data.getPay_method() == 1) {
                                    activity.networkRequest(activity.netApi.apply_pay_main(data.getDeliver_sn()), OrderOperation.this);
                                } else {
                                    activity.networkRequest(activity.netApi.apply_pay_main(data.getDeliver_sn()), OrderOperation.this);
                                }
                            }
                        }, null, false)
                .show();
    }

    private void confirmCancel(OrderGoodsBean data) {
        new XPopup.Builder(activity)
                .asConfirm("提示", "取消确认",
                        "取消", "确定",
                        new OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                activity.networkRequest(activity.netApi.confirm_cancel_apply(data.getDeliver_sn()), OrderOperation.this);
                            }
                        }, null, false)
                .show();
    }

    public void changeStatus(OrderGoodsBean data) {
        String hint = null;
        if (data.getStatus() == 5) {
            hint = "您确定揽收吗？";
        } else if (data.getStatus() == 7) {
            activity.startActivity(new Intent(activity, ShipmentsActivity.class)
                    .putExtra("type", 1)
                    .putExtra("line_id", data.getLine_id())
                    .putExtra("deliver_sn", data.getDeliver_sn()));
            return;
        } else if (data.getStatus() == 11) {
            activity.startActivity(new Intent(activity, SignForActivity.class)
                    .putExtra("deliver_sn", data.getDeliver_sn())
                    .putExtra("type", 1));
            return;
        } else if (data.getStatus() == 13) {
            activity.startActivity(new Intent(activity, SignForActivity.class)
                    .putExtra("deliver_sn", data.getDeliver_sn())
                    .putExtra("type", 2));
            return;
        }

        new XPopup.Builder(activity)
                .asConfirm("提示", hint,
                        "取消", "确定",
                        new OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                if (data.getStatus() == 5) {
                                    activity.networkRequest(activity.netApi.confirmRevenue(data.getDeliver_sn()), OrderOperation.this);
                                }
                            }
                        }, null, false)
                .show();
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean bean = new Gson().fromJson(json, BaseBean.class);
        ToastUtil.show(bean.getMsg());
        if (bean.isStatus()) {
            listener.onClick(null);
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    private MyClickListener listener;

    public void setMyClickListener(@Nullable MyClickListener l) {
        this.listener = l;
    }
}
